package pri.jz.keeper.webservice

import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import pri.jz.keeper.util.HttpUtil
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:用于生产耗资源的client
 */
object WebService {
    val okHttpClient by lazy { loadOkhttp() } //okhttp客户端
    val retrofitClient by lazy { loadRetrofit() } //retrofit客户端

    private fun loadOkhttp(): OkHttpClient = OkHttpClient.Builder()
        .connectTimeout(10,TimeUnit.SECONDS)
        .readTimeout(10,TimeUnit.SECONDS)
        .writeTimeout(10,TimeUnit.SECONDS)
        .build()

    private fun loadRetrofit(): Retrofit = Retrofit.Builder()
        .baseUrl(HttpUtil.serverUrl)
        .client(okHttpClient).apply {
            val gson = GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss").create()
            addConverterFactory(GsonConverterFactory.create(gson))
        }.build()


}